Okay.
So today we're going to have a look at context context action service.
Now it sounds complicated.
It sounds like a complicated thing.
It's got a complex name but it's actually very similar to User Input service.
So hopefully hopefully um, as we did that a few lessons ago now you've had a bit of practice on that
because this is very similar to that.
So the difference between well, context action service service basically solves a problem that user
input service has.
So the issue with user input service is is great for some things but not for other things.
I'll give you some examples.
So um, if you have a button which you hold, hold it down to shift.
That's good for user input service.
You need to do that all the time and very rarely in a game would you not be allowed to shift um for
button open up.
Your inventory is also really good for user input service because you can do it whenever you'd like.
However, let's say when a player enters a car and you want to set up some custom keys in the car.
So some buttons for your indicators and buttons for horn, a button to accelerate, all that other kind
of stuff.
Um, that's where it becomes a bit of an issue, because certain keys have already been taken.
Like you wouldn't be able to use shift to accelerate because you've already used it for sprints.
Um, and also once you even if you're not in the car, those keys are still actively being used for
it.
So you'd most likely, if you don't set up any statements making sure they're not in a car, it will
throw some errors.
So, um, that can be a bit of an issue when you want the player to be able to use a button to do something
in your game, but not all the time, only at specific times.
And that's where Context Action Service comes in.
Um, so you can do it just, just the same with use input service obviously different code, but it's
the same kind of concept.
But you can bind and unbind the action.
And the action is basically the pressing of a key or the inputting or whatever.
So you can, um, allow them to do it and not allow them to do it.
So you can kind of turn it on and off, basically is what you use is what context action service is.
Now, um, this will be on a local script again because we're looking at player input.
So add a local script into starter player scripts and let's take a look.
So it's a service.
So we first need to fetch it.
Um hopefully you should be very confident with getting services already.
Contact action service is equal to game get service.
Contact action service.
Okay, so um, to start off with we just wanted to create a function, um, that occurs when whenever
we do what we want to do.
So let's say um, when they if we do the car example, when they enter a car and when they press the
shift, it accelerates them.
So we can write a function.
Um.
And which will contain the code we want to occur when a certain key runs.
Now call this whatever you want, but I'd recommend if you're looking at just doing it with keys.
I recommend pressing something like um on um on E pressed or something like that there.
Don't call it on button pressed or something like that.
Because if you're wanting to do this for multiple, you're going to be a bit confused which one's which.
Okay.
So it's quite important there to make specific ones for different keys.
And of course if two keys have the same function you could of course set this up just using one.
But um yeah.
So inside here we can do whatever we'd like to happen when they, um, when they press the key.
Um, so.
Um, what could we perhaps do?
Um, we could change something to do with the player.
Perhaps we could maybe change, um, the player's camera mode.
That might be cool.
Or probably leave it for a later lesson.
Actually, we're looking at cameras later on.
Um, let's just make it nice and simple to start off with.
I'll put something else in it.
Let's just print, um, print e it will be a bit more fitting.
Okay.
Just like that there.
So of course, whatever code you want to run in there, that's completely fine.
I've just got a basic one there for examples, but we can have a look a bit more in detail a bit later
on.
Okay, so there are two main functions allowing you to use these.
Um, these are the colon um bind action and colon unbind action.
This activates and deactivates um this input.
So first off you want to bind it.
So bind action like this.
Here lots of parameters you need to put in here.
Um, so even though they've used a car for example here as well.
Um, first also as an action name.
So this basically gives the action a name.
Not a great best explanation of I've done, but um, so the name of what's going on so mean.
Those examples are quite good actually.
If you're wanting for it to be a horn, you could call this honk horn or open a door.
Open door.
It's just whatever it's doing.
So this one is just printing a message.
So I'm going to call it print message like that.
Their function to bind.
You're going to put in there the name of the function you want to bind this to.
Which of course is our on pressed function.
Fair okay.
And um you then want to.
So there's also this option here to create a touch button.
So a touch button basically allows.
Um this is one thing that use input service doesn't doesn't do as well as contact action service actually,
which is a um a mobile button.
So mobile players are able to um, use this as well as keyboard players.
So if you are interested in making your game able to be played on tablets and stuff, you might want
to consider using Context Action Service.
You can set it up and use input service.
Absolutely.
Um, I just don't think you have as much control over it as you do with context action.
Um, let's let's put it to two so I can show you what it looks like.
And then here is the, um, the key code, um, that, that you want to use.
So this one we write as a string.
Okay.
Um, or we could probably write it as enum dot key code.
Um, any number of um, okay.
Let's I used it originally with a string, but I think it might be a bit better if we use it with this.
Let's see if this works if we use an enum.
So obviously you want this to work with E.
Like that.
Okay.
And obviously after a bit of time, we want to unbind the action.
And that's very simple.
Contraction service.
Colon.
Unbind action.
And here you just need to put the name in and unbind it for you.
So unbind.
Print message like that.
There.
Okay.
So let's say that we wait, um, five seconds.
In order to do this, you then have a ten second slot where you can do this, and then it unbinds it
just so we can see the idea of it being active and not being active.
So let's play the game and try this out.
Um, so we're in the game here.
Um, I'm pressing E and it's not working, and now it is working.
It's now inputting.
Nice.
That's nice.
And hopefully after a bit of time, it will then stop.
Um.
Bring it on back.
There we go.
It's now unbounded reaction, so I can no longer do it.
You can hear me.
I'm pressing the key.
I probably can't hear me, but I'm pressing the key and it's not working.
Okay.
Um.
Cool.
So think now of the data store script in here.
Yeah.
Delete that.
Whoops.
Um, okay, so um, as for the button, we couldn't actually see the button.
And that's because we need to set it, give it a position in order to be able to see it.
So to set to give the button a position we will need to say content action service.
And this is another function called colon set position.
And to set position of your touch button.
So you need to put the action name which is print message.
We are in any position.
So, um, I've got this position down as a good position to use, but feel free to change this around.
Um, so 0.7, -25, 0.7.
Um, -25.
So let's see what that might look like.
Okay.
So let's give this a go and see what this button mobile button might look like.
So after a few seconds, um, hopefully be able to see um.
Oh.
Not appearing.
Do we need to go into?
Um.
Might not be appearing.
G player.
G.
Okay.
Let's see.
Why might this not be working?
Um.
I definitely set this to be true.
Um, what if we change this to E?
Would that work?
Because it knows what button to press, perhaps.
Um.
If not, I think I've got an idea as to why this might not be working.
Okay.
So I think the issue with this is that we need to change the, um, the device we're on perhaps.
So I've now changed the test to be a phone.
So this is what it would look like if a player on an iPhone ten would be doing this.
And you play here, maybe now it would show it up because it's now registered it as a device.
So after a few seconds, will it appear on my screen?
There we go.
It now appears okay I did it right.
Okay.
So when you click this button it will work just normally.
Okay.
Um, perfect.
And you can also change all kinds of things you like with this button.
So contact action service.
Um wrong keyword.
Um colon set so you can set position, set image, set title and other stuff.
So I'm going to give it a title.
Let's do that as well.
So the action name is the print message okay.
The title would be something like um e like that.
So we know what button it is.
And if we play the game.
Hopefully it should appear there as just E.
In a few seconds yet.
There we go.
Perfect.
So it's a really good method for your for your game to be able to work on other devices as well.
Now.
Um, yeah, obviously the button wasn't perfect as it covered the jump button a bit.
So definitely have a rework of those and try out which one you think would be the best for yourself.
Just a bit of trial and error really with that.
Um, now some possibilities of this.
I think maybe playing a sound might be quite a cool one to do.
So um, let's go over to toolbox and find a sounds, um, horn.
Let's just pick any one insert and let's copy the, um, ID here.
We don't really need the ID, actually.
We can put it in a local script and just reference it and do it that way.
So local sound is equal to script dot.
And I'm going to rename this just to be horn.
Let's preview it.
Cool.
Um, and let's go back to script.
Script horn instead of printing message.
Let's let's just go sound colon play like that there and I'll leave it.
I'll leave it called print message just for simplicity, but, um.
Yeah.
Or actually let's use the replace function.
Print message.
Let's change this with car horn.
Cool.
So now when I press E it will play that really loud sound.
I'm going to turn this down a lot.
Um.
It's a bit more easy to manage.
And let's go ahead and just try this out.
So for a few seconds that we can do it by press E!
There we go doing that car horn.
It's quite a long horn.
Um, and again, the advantage of doing it this way is it's a lot easier to set up this button for mobile
device, because we then go into it using the mobile device.
It will appear.
And we can then use it.
So when we when we binded the action, that's when the car horn button appeared.
So obviously you could do it.
So when the player enters the car, when they go into the seat for the car, then you can then bind
all these different actions.
So it's really good because you can bind and unbind all these actions which do whatever here, basically.
Okay, I think Qantas Action Service is a really cool service for you to use.
And while it doesn't make user input service obsolete because.
I think because you had to bind and unbind doesn't necessarily make it obsolete, but still use userinputservice.
But just be careful when you are using it just to make sure.
Is is this best suited for use in this service, or should I use Context Action service instead as well?
Okay.
Um, and if if it's something that be small, like for example, the sprinting argument I made earlier,
um, you might not want to be able to sprint if you, um, if you are, um, I don't know, in, in
a certain area of the map, then it'd be easier.
Then instead of making a whole bind and unbind action, maybe you just make an if statement to check.
Are we in the area of your map?
But you can use either way.
Either way you can use use that to get it to make to be that function.
It's just up to you.
And if you want to use each one so you don't necessarily have to use it, you can just stick with Userinputservice,
but I definitely recommend it for some examples.
Okay, so hope you enjoyed having a look at a bit more of a variant I guess of Userinputservice.
And next lesson we are going to have a look at something quite exciting.
We're going to learn how to make game passes and add those into our game, so stay tuned for that one.